﻿Imports BUS
Imports DTO
Imports System.Runtime.InteropServices
Public Class FormQLNhanVien
    Private Sub FormQLNhanVien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load()
    End Sub
    Public Sub Load()
        Dim dtNV As DataTable
        Dim nvBUS As New BUS.NhanVienBUS()
        dtNV = nvBUS.LoadNV()
        gcNhanVien.DataSource = dtNV
    End Sub
    Private Sub btnCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhat.Click
        Try
            Dim dt As DataTable
            dt = gcNhanVien.DataSource
            For Each row As DataRow In dt.Rows
                If (row.RowState = DataRowState.Modified) Then
                    Dim nv As New DTO.DTONhanVien()
                    Integer.TryParse(row(0).ToString(), nv._iMaNV)
                    nv._sTen = row(1).ToString()
                    nv._sDiaChi = row(2).ToString()
                    nv._sCMND = row(4).ToString()
                    nv._sGioiTinh = row(5).ToString()
                    Dim nvBus As New BUS.NhanVienBUS
                    nvBus.UpdateNV(nv)
                    MessageBox.Show("Thành công!", "Thông báo")
                End If
                If (row.RowState = DataRowState.Added) Then
                    Dim nv As New DTO.DTONhanVien()
                    nv._sTen = row(1).ToString()
                    nv._sDiaChi = row(2).ToString()
                    nv._sCMND = row(4).ToString()
                    nv._sGioiTinh = row(5).ToString()
                    Dim nvBus As New BUS.NhanVienBUS
                    nvBus.AddNV(nv)
                    Load()
                    MessageBox.Show("Thành công!", "Thông báo")
                End If
            Next
        Catch ex As Exception
            MessageBox.Show("Không thực được thao tác !", "Lỗi !", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    'Tham khảo code http://diendan.congdongcviet.com/showthread.php?p=223874
    Private Sub gcNhanVien_ProcessGridKey(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles gcNhanVien.ProcessGridKey
        Dim dt As DataTable
        dt = gcNhanVien.DataSource
        If e.KeyCode = Keys.Delete Then
            If GridView1.FocusedRowHandle < 0 Then
                MessageBox.Show("Không thể xóa dòng này!!!???", "Lỗi !", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                If MessageBox.Show("Bạn có chắc chắn muốn xóa???", "Chú ý !", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    Dim dr As DataRow
                    dr = dt.Rows(GridView1.FocusedRowHandle)
                    If dr(3) = 2 Then
                        Dim ctBUS As New BUS.CaTrucBUS
                        ctBUS.KetThucCaTruc(dr(0).ToString())
                    End If
                    Dim nv As New DTO.DTONhanVien()
                    Integer.TryParse(dr(0).ToString(), nv._iMaNV)
                    Dim nvBus As New BUS.NhanVienBUS
                    nvBus.DelNV(nv)

                    'dt.Rows.Remove(dr)
                    dt.AcceptChanges()
                    Load()
                End If
            End If
        End If

    End Sub

    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
        Load()
    End Sub

    Private Sub btnNVCu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNVCu.Click
        Try
            Dim ma As String
            ma = GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "MaNV")
            Dim nv As New DTO.DTONhanVien()
            Integer.TryParse(ma, nv._iMaNV)
            If (GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "TinhTrang") <> 3) Then
                MessageBox.Show("Nhân viên này không đúng ", "Chú ý !", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Else
                Dim nvBus As New BUS.NhanVienBUS
                nvBus.NVCu(nv)
                Load()
                MessageBox.Show("Thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MessageBox.Show("Không thể thực hiện thao tác ", "Lỗi !", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try


    End Sub
End Class